import tkinter as tk
def click():
    txt4.delete(0,"end")
    n=int(txt1.get())
    a = list(map(int, txt2.get().split()))
    key=int(txt3.get())
    i=0
    j=(n+1)//2
    flag=False
    while i<=j and flag==False:
        m=(i+j)//2
        if key==a[m]:
            flag=True
        elif key<a[m]:
            j=m-1
        else:
            i=m+1
    if flag==False and j>=0:
        m=abs(j-n+1)
        if key==a[m]:
            flag=True
    if flag==True:
        txt4.insert(0,"查找的数在第"+str(m+1)+"个")
    else:
        txt4.insert(0,"找不到")
                
win=tk.Tk()
win.title('数字查找')
win.geometry ('300x150')
win.resizable(200,200)

btn = tk.Button(win,text='确定',command=click)
lb1 = tk.Label(win,text='请输入数组个数：')
lb2= tk.Label(win,text='请输入数组元素:')
lb3= tk.Label(win,text='请输入要查找的数组元素')
lb4=tk.Label(win,text='结果：')
txt1 = tk.Entry(win)
txt2 = tk.Entry(win)
txt3 = tk.Entry(win)
txt4=tk.Entry(win)

txt1.grid(row=2,column=2)
txt2.grid(row=4,column=2)
txt3.grid(row=6,column=2)
txt4.grid(row=8,column=2)
lb1.grid(row=2,column=1)
lb2.grid(row=4,column=1)
lb3.grid(row=6,column=1)
lb4.grid(row=8,column=1)
btn.grid(row=10,column=2,columnspan=2,pady=10)

win.mainloop()
          
